﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System.Linq;
using System.Collections.Generic;
using System.Windows.Controls;
using System.ComponentModel;

using MissedMemo.MeatAndCheeseDisc.OrderEntryDataAccess;
using MissedMemo.MeatAndCheeseDisc.OrderEntryService.OrderProcessing;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntry
{
    public partial class viewAddEditPizza : UserControl
    {
        public viewAddEditPizza()
        {
            InitializeComponent();

            if( false == DesignerProperties.GetIsInDesignMode( this ) )
            {
                foreach( string strPizza in LookupMenuItem.GetTypes( "Pizza" ) )
                    comboPizzaType.Items.Add( strPizza );

                // Request sizes for any pizza type (Pizza sizes do not vary)
                string strType = comboPizzaType.Items[0].ToString();

                foreach( string strSize in LookupMenuItem.GetSizes( "Pizza", strType ) )
                    comboPizzaSize.Items.Add( strSize );
            }
        }


        // Update list of available options, based on the type and size Pizza the user has selected
        // (toppings only apply to 'Custom' pizzas, 'Deep Dish' is invalid for 'Mini' pizzas etc.)

        private void comboPizzaTypeOrSize_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            Pizza pizza = DataContext as Pizza;

            if( pizza != null )
            {
                // Get ALL options that apply to current size & type
                IEnumerable<string> validOptions = LookupMenuItem.GetOptionsAndToppings( pizza.Type, pizza.Size );


                // Update AVAILABLE options (excluding any already applied)

                listOptionsAvailable.Items.Clear();

                foreach( string str in validOptions )
                {
                    if( null == pizza.Options.FirstOrDefault( s => s == str ) )
                    {
                        listOptionsAvailable.Items.Add( str );
                    }
                }


                // Update APPLIED options (removing those invalid under current size & type)

                List<string> invalidOptions = new List<string>();

                foreach( string str in pizza.Options )
                {
                    if( null == validOptions.FirstOrDefault( s => s == str ) )
                    {
                        invalidOptions.Add( str );
                    }
                }

                foreach( string str in invalidOptions )
                    pizza.Options.Remove( str );
            }
        }


        // Apply an option to the current pizza

        private void btnAddOption_Click( object sender, System.Windows.RoutedEventArgs e )
        {
            Pizza pizza = DataContext as Pizza;

            if( pizza != null )
            {
                string strOption = listOptionsAvailable.SelectedItem as string;

                pizza.Options.Add( strOption );

                listOptionsAvailable.Items.Remove( strOption );
            }
        }


        // Remove an option from the current pizza 

        private void btnRemoveOption_Click( object sender, System.Windows.RoutedEventArgs e )
        {
            Pizza pizza = DataContext as Pizza;

            if( pizza != null )
            {
                string strOption = listOptionsApplied.SelectedItem as string;

                pizza.Options.Remove( strOption );

                listOptionsAvailable.Items.Insert( 0, strOption );
            }
        }
    }
}
